home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1996 / MacHack 1996.toast / Hacks / Hacks ’89 / Gas Gauge / Windows / windows_main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-06-16  |  8.4 KB  |  396 lines  |  [TEXT/KAHL]

  1. /*             Copyright (c) 1988 by Caldera Corporation                        */
  2.     
  3. /*            PO box 3252, Ann Arbor, MI 48106   (313) 996-9059                */
  4.  
  5. /*        File:         windows_main.c                                            */
  6. /*        Compilers:     LightSpeedC 3.0, Aztec C 3.0                            */
  7.  
  8. /*        Abstract:                                                            */
  9. /*            high level and code segment 1 routines for windows                */
  10.  
  11. /*        History:                                                            */
  12. /*            2/10/89 1.0d1 - released final 1.0 version                         */
  13. /*            6/15/89 1.0d2 - hacked up for public release as part of MacHack    */
  14.  
  15.  
  16. #include    <Compatability.h>            /* handle compiler differences        */
  17. #include    <StdInclude.h>                /* include required headers, etc    */
  18. #include    "windows.h"                    /* include globals, prototypes, etc    */
  19.  
  20. MenuHandle    menuHdls[MAXMENUS+1];                /* array of menu handles    */
  21. Boolean        done;                                  /* quit program flag        */
  22. EventRecord    myEvent;                               /* app event record            */
  23. WindowRecord wRecs[MAXWINDOWS];                    /* array of window records    */
  24. WindowPtr    wPtrs[MAXWINDOWS];                    /* array of window pointers    */
  25. Rect        dragRect;                            /* drag window bounds        */
  26. Rect        limitRect;                            /* resize window bounds        */
  27.  
  28.  
  29. OSErr DoAppleCmds( theItem )
  30. int        theItem;
  31. {
  32.     char    name[256];                            /* string for DA name        */
  33.     OSErr    error;                                /* error returned by open    */
  34.     GrafPtr    tempPtr;                            /* original GrafPort ptr    */
  35.  
  36.     switch(theItem) 
  37.     {
  38.         case appleABOUT: 
  39.             
  40.             return( miscErr );
  41.             break;
  42.  
  43.         default:
  44.             GetPort( &tempPtr );
  45.             GetItem( menuHdls[1], theItem, name );    /* get the DA item/name    */
  46.             error = OpenDeskAcc( name );        /* open the desk accessory    */
  47.             SetPort( tempPtr );                    /* reset grafport to original*/
  48.     }
  49.     return( noErr );
  50. }
  51.  
  52.  
  53. OSErr DoFileCmds ( theItem )
  54. int        theItem;
  55. {
  56.     switch( theItem ) 
  57.     {
  58.         case fileQUIT: 
  59.             done = TRUE;                    /* set program done flag to TRUE*/
  60.             break;
  61.  
  62.         default:
  63.             return( miscErr );
  64.     }
  65.     return( noErr );
  66. }
  67.  
  68.  
  69. OSErr DoOptionCmds( theItem )
  70. int        theItem;
  71. {
  72.     WindowPtr    wPtr;
  73.     int            i;
  74.  
  75.     switch( theItem ) 
  76.     {
  77.         case ADD_ITEM: 
  78.             AppendMenu( GetMHandle( OPTIONMENU ), "\PAppended item" );
  79.             break;
  80.             
  81.         case NEW_WINDOW:
  82.             wPtr = AllocWindow();
  83.             break;
  84.  
  85.         default:
  86.             return( miscErr );
  87.     }
  88.     return( noErr );
  89. }
  90.  
  91.  
  92. OSErr Dispatch ( menuResult )
  93. long    menuResult;
  94. {
  95.     int        theMenu;                                /* menu selected        */
  96.     int        theItem;                                   /* item selected        */
  97.     int        error;                                   /* error returned        */
  98.  
  99.     theMenu = HiWord( menuResult );                 /* menuID selected        */
  100.     theItem = LoWord( menuResult );                 /* item# selected        */
  101.     
  102.     switch(theMenu) {
  103.         case APPLEMENU: 
  104.             error = DoAppleCmds( theItem );
  105.             break;
  106.  
  107.         case FILEMENU: 
  108.             error = DoFileCmds( theItem );
  109.             break;
  110.  
  111.         case OPTIONMENU: 
  112.             error = DoOptionCmds( theItem );
  113.             break;
  114.             
  115.         default:
  116.             HiliteMenu( 0 );                       /* un-hilite selected menu     */
  117.             return( miscErr );
  118.     }
  119.     HiliteMenu( 0 );                               /* un-hilite selected menu     */
  120.     return( noErr );
  121. }
  122.  
  123.  
  124. void DoKey( keyMsg, keyMods )
  125. long    keyMsg;
  126. short    keyMods;
  127. {
  128.     char    keyPressed;
  129.  
  130.     keyPressed = (char)( BitAnd( keyMsg, charCodeMask ));
  131.     if( BitAnd( keyMods, cmdKey ) != 0 )
  132.         Dispatch( MenuKey ( keyPressed ));            /* Command key down        */
  133. }
  134.  
  135.  
  136. OSErr DoUpdate( wPtr )
  137. WindowPtr    wPtr;
  138. {
  139.     GrafPtr        tempPtr;
  140.     
  141.     if ( wPtr == NULL)
  142.         return ( nilHandleErr );
  143.         
  144.     GetPort( &tempPtr );            /* save current active grafPort         */
  145.     SetPort( wPtr );                 /* make update window active grafPort    */
  146.     ClipRect ( &(wPtr->portRect) );    /* make sure clipRect is window sized    */
  147.     BeginUpdate( wPtr );            /* visRgn temporarily = updateRgn        */
  148.         if( HasGrow ( wPtr ))
  149.             DrawGrowIcon( wPtr );    /* update grow icon appropriately        */
  150.         DrawControls ( wPtr );
  151.     EndUpdate( wPtr );                 /* restore normal visRgn of grafport    */
  152.     SetPort( tempPtr );                /* restore original grafPortwhen done    */
  153.     return ( noErr );
  154. }
  155.  
  156.  
  157. OSErr DoActivate ( wPtr )
  158. WindowPtr    wPtr;
  159. {
  160.     ControlHandle    the_ctl;
  161.     
  162.     if ( wPtr == NULL)
  163.         return ( nilHandleErr );
  164.         
  165.     if( HasGrow ( wPtr ))
  166.         DrawGrowIcon( wPtr );        /* update grow icon appropriately        */
  167.     if(BitAnd(myEvent.modifiers,1) != 0)
  168.     {
  169.         SetPort( wPtr );
  170.         ClipRect ( &(wPtr->portRect) );    /* make sure clipRect window sized    */
  171.         the_ctl = (ControlHandle)GetWRefCon ( wPtr );
  172.         if ( the_ctl != NULL )
  173.             HiliteControl ( the_ctl, 0 );
  174.     }
  175.     else
  176.     {
  177.         the_ctl = (ControlHandle)GetWRefCon ( wPtr );
  178.         if ( the_ctl != NULL )
  179.             HiliteControl ( the_ctl, 255 );
  180.     }
  181.     return ( noErr );
  182. }
  183.  
  184.  
  185. OSErr DoMouseDown ( globalPt )
  186. Point        globalPt;
  187. {
  188.     int                windowPart;        /* part of window or desktop where clicked    */
  189.     WindowPtr        whichWindow;    /* pointer to window where mouse was clicked*/
  190.     long            result;
  191.     ControlHandle    the_ctl;
  192.     int                the_part;
  193.     pascal void        MyTracker();
  194.     
  195.     windowPart = FindWindow(globalPt, &whichWindow);
  196.  
  197.     switch(windowPart) 
  198.     {
  199.         case inMenuBar: 
  200.             Dispatch( MenuSelect( globalPt ));
  201.             break;
  202.  
  203.         case inSysWindow:  
  204.             SystemClick( &myEvent, whichWindow );        /* send to DA        */
  205.             break;
  206.  
  207.         case inGoAway:
  208.             if ( TrackGoAway( whichWindow, globalPt ))    /* if cursor in box    */
  209.             {
  210.                 if ( BitAnd( myEvent.modifiers, optionKey )) /* option key    */
  211.                     while ( FrontWindow() != NULL )
  212.                         DeAllocWindow( FrontWindow() );/* close all windows    */
  213.                 else
  214.                     DeAllocWindow( whichWindow );    /* close specific window*/
  215.             }
  216.             break; 
  217.  
  218.         case inDrag:
  219.             DragWindow( whichWindow, globalPt, &dragRect ); 
  220.             break; 
  221.  
  222.         case inZoomIn:
  223.         case inZoomOut:
  224.             if ( TrackBox( whichWindow, globalPt, windowPart ))
  225.                 ZoomWindow( whichWindow, windowPart, TRUE );
  226.             if( HasGrow ( whichWindow ))
  227.             {
  228.                 SetPort ( whichWindow );
  229.                 ClipRect ( &(whichWindow->portRect) );
  230.                 DrawGrowIcon( whichWindow );            /* update grow icon    */
  231.             }
  232.             break;
  233.  
  234.         case inGrow:
  235.             result = GrowWindow( whichWindow, globalPt, &limitRect );
  236.             SizeWindow( whichWindow, LoWord( result ), HiWord( result ), TRUE );
  237.             if( HasGrow ( whichWindow )) 
  238.             {
  239.                 SetPort ( whichWindow );
  240.                 ClipRect ( &(whichWindow->portRect) );
  241.                 DrawGrowIcon( whichWindow );            /* update grow icon    */
  242.             }
  243.             break; 
  244.  
  245.         case inContent: 
  246.             if(whichWindow != FrontWindow())
  247.                 SelectWindow(whichWindow);
  248.             else
  249.             {
  250.                 GlobalToLocal ( &globalPt );
  251.                 if ( the_part = FindControl( globalPt, whichWindow, &the_ctl ) )
  252.                 {
  253.                     if ( the_part == inThumb )
  254.                         TrackControl ( the_ctl, globalPt, NULL );
  255.                     else
  256.                         TrackControl ( the_ctl, globalPt, MyTracker );
  257.                 }
  258.             }
  259.             break; 
  260.  
  261.         default:
  262.             return( miscErr );
  263.     }
  264.     return ( noErr );
  265. }
  266.  
  267.  
  268. void main( )
  269. {
  270.     MaxApplZone();
  271.     MoreMasters();                               /* create more master pointers    */
  272.     MoreMasters();                               /* create more master pointers    */
  273.     MoreMasters();                               /* create more master pointers    */
  274.     MoreMasters();                               /* create more master pointers    */
  275.  
  276.     Init_all();                                /* call initialization routine    */
  277.     UnloadSeg( &Init_all );                    /* make segment purgable        */
  278.  
  279.     do                                        /* main event loop                */
  280.     {
  281.         SystemTask();                        /* let DAs have some CPU cycles    */
  282.         if(GetNextEvent( everyEvent, &myEvent )) 
  283.         {
  284.             switch( myEvent.what ) 
  285.             {
  286.                 case mouseDown: 
  287.                     DoMouseDown( myEvent.where );
  288.                     break;
  289.  
  290.                 case keyDown:
  291.                 case autoKey: 
  292.                     DoKey( myEvent.message, myEvent.modifiers );
  293.                     break;
  294.  
  295.                 case activateEvt: 
  296.                     DoActivate( (WindowPtr) myEvent.message );
  297.                     break;
  298.  
  299.                 case updateEvt : 
  300.                     DoUpdate( (WindowPtr) myEvent.message );
  301.                     break;
  302.             }
  303.         }
  304.     } while (!done);
  305. }
  306.  
  307.  
  308. void DeAllocWindow( wPtr )
  309. WindowPtr    wPtr;
  310. {
  311.     int            i;
  312.     
  313.     if ( wPtr == NULL )
  314.         return;
  315.     
  316.     for (i=0; i<MAXWINDOWS; i++) 
  317.     {
  318.         if ( wPtrs[i] == wPtr ) 
  319.         {
  320.             DisposeWindow( wPtr );
  321.             wPtrs[i] = NULL;
  322.             return;
  323.         }
  324.     }
  325. }
  326.  
  327.  
  328. WindowPtr AllocWindow()
  329. {
  330.     int                i;
  331.     ControlHandle    the_ctl_hdl;
  332.     int                wType;
  333.     
  334.     wType = 4;
  335.     for ( i=0; i<MAXWINDOWS; i++ ) 
  336.     {
  337.         if ( wPtrs[i] == NULL ) 
  338.         {
  339.             wPtrs[i] = GetNewWindow( RESID_BASE + wType, &(wRecs[i]), (Ptr)-1 );
  340.             if ( wPtrs[i] != NULL ) 
  341.             {
  342.                 if (( wType == 0 ) || ( wType == 8 ))
  343.                     SetGrow( wPtrs[i], TRUE );
  344.                 else
  345.                     SetGrow( wPtrs[i], FALSE );
  346.                 the_ctl_hdl = GetNewControl ( 256, wPtrs[i] );
  347.                 if ( the_ctl_hdl != NULL)
  348.                     SetWRefCon ( wPtrs[i], (long)the_ctl_hdl );
  349.             }
  350.             return(wPtrs[i]);
  351.         }
  352.     }
  353.     return(NULL);
  354. }
  355.  
  356.  
  357. Boolean HasGrow( wPtr )                /* determines whether or not a window    */
  358. WindowPtr    wPtr;                    /* has a grow box, returns TRUE for yes    */
  359. {
  360.     return ( FALSE );
  361. }
  362.  
  363.  
  364. void SetGrow( wPtr, flag )
  365. WindowPtr    wPtr;
  366. Boolean        flag;
  367. {
  368. }
  369.  
  370.  
  371. pascal void    MyTracker ( the_ctl, the_part )
  372. ControlHandle    the_ctl;
  373. int                the_part;
  374. {
  375.     int    amt;
  376.     
  377.     switch ( the_part )
  378.     {
  379.         case inUpButton:
  380.             amt = -1;
  381.             break;
  382.         case inPageUp:
  383.             amt = -10;
  384.             break;
  385.         case inPageDown:
  386.             amt = 10;
  387.             break;
  388.         case inDownButton:
  389.             amt = 1;
  390.             break;
  391.         default:
  392.             amt = 0;
  393.     }
  394.     SetCtlValue ( the_ctl, GetCtlValue ( the_ctl ) + amt );
  395. }
  396.